package com.wangyadong.hobby.thread;

import java.time.LocalDateTime;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;
import java.util.stream.IntStream;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/7/110:52
 **/
public class SheduledThreadPoolExecutorTest {

    public static void main(String[] args) throws InterruptedException {

//        TimerTask timer = timerTaskAtFixedRateTest();
//        TimerTask timer = timerTaskTest();
//        TimerTask timer = reaptTaskTimerTest();
//        ScheduledThreadPoolExecutor executor = testScheduledThreadPool();
//        ScheduledThreadPoolExecutor executor = testScheduledThreadPoolWithRepeate();
        ScheduledThreadPoolExecutor executor = testScheduledThreadPoolWithFuture();
        while (true) {
//            long executionTime = timer.scheduledExecutionTime();
//            System.out.println("executor time ->" + executionTime);
//            TimeUnit.SECONDS.sleep(1);
//            boolean cancel = timer.cancel();
//            System.out.println("cancel status ->" + cancel);

            //


        }
    }

    /**
     * the reapt task
     * if call a reapt task cancel method always return true
     *
     * @return
     */
    public static TimerTask reaptTaskTimerTest() {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println(LocalDateTime.now());
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Timer timer = new Timer(true);
        timer.schedule(timerTask, 2000, 2000);
        return timerTask;
    }

    /**
     * TimerTask no reapte task
     * has aleady run or current runnning  call  cancel method return false
     *
     * @return
     */
    public static TimerTask timerTaskTest() {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                try {
                    System.out.println(LocalDateTime.now());
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        };
        Timer timer = new Timer();
        timer.schedule(timerTask, 2000);
        return timerTask;
    }


    public static TimerTask timerTaskAtFixedRateTest() {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                try {
                    System.out.println(LocalDateTime.now());
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        };
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(timerTask, 2000, 1000);
        return timerTask;
    }


    public static ScheduledThreadPoolExecutor testScheduledThreadPool() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);
        IntStream.range(0, 10).forEach(i -> {
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("index is ->" + i);
                }
            });
        });
        executor.shutdownNow();
        System.out.println("shut down");
        return executor;
    }

    public static ScheduledThreadPoolExecutor testScheduledThreadPoolWithRepeate() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);
        executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
//        executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
        IntStream.range(0, 10).forEach(i -> {
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("index is ->" + i);
                }
            });
        });

        executor.shutdown();
        System.out.println("shut down");
        return executor;
    }


    public static ScheduledThreadPoolExecutor testScheduledThreadPoolWithFuture() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);
        executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
//        executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
        IntStream.range(0, 10).forEach(i -> {
            Future<Object> submit = executor.submit(new Callable<Object>() {

                public Object call() {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("index is ->" + i);
                    return i;
                }
            });
            try {
                System.out.println("subit-->" + submit.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });

        executor.shutdown();
        System.out.println("shut down");
        return executor;
    }
}
